package com.wanmait.travel.controller.manager;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.travel.dao.ManagerDAO;
import com.wanmait.travel.vo.Comment;
import com.wanmait.travel.vo.Manager;
import com.wanmait.travel.vo.Scenic;
import com.wanmait.travel.vo.UserPay;

@WebServlet("/ManagerController")
public class ManagerController extends HttpServlet
{
	private ManagerDAO managerDAO =ManagerDAO.getDAO();

	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
	
		request.setCharacterEncoding("UTF-8");
		String action =request.getParameter("action");
		switch(action) {
		  case "login":
			  this.login(request,response);
			  break;
		  case "userC":
			  this.userComment(request,response);
			  break;
		  case "managerShow":
			  this.managerShow(request,response);
			  break;
		  case "scenicAllComment":
			  this.scenicAllComment(request,response);
			  break;
		  case "userComment":
			  this.userComment(request,response);
			  break;
		  case "searchComment":
			  this.searchComment(request,response);
			  break;
		  case "userCommentSearch":
			  this.userCommentSearch(request,response);
			  break;
		  case "userPay":
			  this.userPay(request,response);
			  break;
		}
	}
	
	
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{	
		String userName =request.getParameter("userName");
		String userPass =request.getParameter("userPass");
		Manager  manager = managerDAO.findByNameAndPass(userName,userPass);
		if(manager==null) 
		{
			response.sendRedirect("/Travel/manager/login.jsp?err=1");
		}
		else {
			HttpSession session= request.getSession();
			session.setAttribute("manager",manager);
			response.sendRedirect("/Travel/scenic/main.jsp");
		}
	}
	
	protected void scenicAllComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{	
		List scenicAllComment=managerDAO.scenicAllComment();
		request.setAttribute("scenicAllComment", scenicAllComment);
		request.getRequestDispatcher("/manager/userC.jsp").forward(request, response);
	}
	
	protected void userComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{	
		String scenicIdStr=request.getParameter("scenicId");
		Integer scenicId=Integer.parseInt(scenicIdStr);
		List<Comment> comments=managerDAO.scenicCommentShow(scenicId);
		request.setAttribute("comments",comments);
		request.getRequestDispatcher("/manager/comment.jsp").forward(request, response);
	}
	
	
	
	protected void managerShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{	
		String managerIdStr=request.getParameter("managerId");
		Integer managerId=Integer.parseInt(managerIdStr);
		List<Scenic> scenicShow=managerDAO.managerFindAll(managerId);
		request.setAttribute("scenicShow", scenicShow);
		request.getRequestDispatcher("/manager/scenicShow.jsp").forward(request, response);
	}
	
	protected void searchComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{	
		request.setCharacterEncoding("UTF-8");
		String searchCommentStr=request.getParameter("searchComment");
		List<Comment> searchComment=managerDAO.searchComment(searchCommentStr);
		request.setAttribute("searchComment", searchComment);
		request.getRequestDispatcher("/manager/searchcommentshow.jsp").forward(request, response);
	}
	protected void userCommentSearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{	
		request.setCharacterEncoding("UTF-8");
		String userIdStr=request.getParameter("userId");
		Integer userId=Integer.parseInt(userIdStr);
		List<Comment> userComment=managerDAO.userComment(userId);
		request.setAttribute("comments", userComment);
		request.getRequestDispatcher("/manager/comment.jsp").forward(request, response);
	}
	protected void userPay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{	
		request.setCharacterEncoding("UTF-8");
		String userIdStr=request.getParameter("userId");
		Integer userId=Integer.parseInt(userIdStr);
		List<UserPay> userPays=managerDAO.userPay(userId);
		request.setAttribute("userPays", userPays);
		request.getRequestDispatcher("/manager/userPay.jsp").forward(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
}	
